package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.shop.dao.ShopAddressDao;
import ysq.xyg.shop.model.po.ShopAddress;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopAddressVo;
import ysq.xyg.shop.service.ShopAddressService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商城模块-用户地址service实现
 *
 * @author congcong tao
 * @date 2019-5-27 7:34:48
 */
@Service("shopAddressService")
public class ShopAddressServiceImpl extends BaseServiceImpl<ShopAddress> implements ShopAddressService {

    @Resource
    private ShopAddressDao shopAddressDao;

    private ShopAddress shopAddress;

    @Autowired
    public void setBaseDao(ShopAddressDao shopAddressDao) {
        this.baseDao = shopAddressDao;
    }

    /**
     * 分页查询
     *
     * @param shopAddressVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-27 7:34:48
     */
    @Override
    public ReturnEntity page(ShopAddressVo shopAddressVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopAddress> list = acceptList(shopAddressVo);
        PageInfo<ShopAddress> page=new PageInfo<ShopAddress>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopAddressVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-27 7:34:48
     */
    @Override
    public ReturnEntity addOrUpdate(ShopAddressVo shopAddressVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopAddressVo.getId())){
            shopAddress=new ShopAddress(currentUser);
            isUpate=false;
        }else{
            shopAddress=selectByPrimaryKey(shopAddressVo.getId());
            if(ObjectUtil.isNull(shopAddress)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopAddress.setUpdateTime(new Date());
            shopAddress.setUpdateUser(currentUser.getId());
        }
        shopAddress.setName(shopAddressVo.getName());
        shopAddress.setDoorType(shopAddressVo.getDoorType());
        shopAddress.setBuilding(shopAddressVo.getBuilding());
        shopAddress.setUnit(shopAddressVo.getUnit());
        shopAddress.setRoom(shopAddressVo.getRoom());
        shopAddress.setDescripts(shopAddressVo.getDescripts());
        shopAddress.setType(shopAddressVo.getType());
        shopAddress.setOrderNum(shopAddressVo.getOrderNum());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopAddress);
        }else{
            num=insert(shopAddress);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-27 7:34:48
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopAddress shopAddress=selectByPrimaryKey(id);
        if(shopAddress!=null){
            shopAddress.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopAddress.setUpdateTime(new Date());
            shopAddress.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopAddress);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopAddressVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-27 7:34:48
     */
    @Override
    public List<ShopAddress> acceptList(ShopAddressVo shopAddressVo){
        Example example = new Example(ShopAddress.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopAddressVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopAddressVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopAddressDao.selectByExample(example);
    }

    @Override
    public ReturnEntity setAddressDefault(String memberId, String addressId) {
        ShopAddress address = this.selectByPrimaryKey(addressId);
        address.setType(BaseConstant.DEFAULT_ADDRESS_YES);
        this.updateByPrimaryKeySelective(address);
        shopAddressDao.updateDefaultType(memberId, address.getId());
        // this.updateDefaultType(address.getId());
        return new ReturnEntity();
    }

    @Override
    public ReturnEntity updateOrAddAddress(ShopAddressVo shopAddressVo) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopAddressVo.getId())){
            shopAddress = new ShopAddress();
            shopAddress.setId(BaseUtil.uId());
            shopAddress.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
            shopAddress.setCreateTime(DateUtil.currentDate());
            shopAddress.setType("2");
            shopAddress.setUpdateTime(shopAddress.getCreateTime());
            isUpate=false;
        }else{
            shopAddress=selectByPrimaryKey(shopAddressVo.getId());
            if(ObjectUtil.isNull(shopAddress)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopAddress.setUpdateTime(new Date());
        }
        shopAddress.setMemberId(shopAddressVo.getMemberId());
        shopAddress.setLinkName(shopAddressVo.getLinkName());
        shopAddress.setPhone(shopAddressVo.getPhone());
        shopAddress.setName(shopAddressVo.getName());
        shopAddress.setDoorType(shopAddressVo.getDoorType());
        shopAddress.setBuilding(shopAddressVo.getBuilding());
        shopAddress.setUnit(shopAddressVo.getUnit());
        shopAddress.setRoom(shopAddressVo.getRoom());
        shopAddress.setDescripts(shopAddressVo.getDescripts());
        shopAddress.setType(shopAddressVo.getType());
        shopAddress.setOrderNum(shopAddressVo.getOrderNum());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKeySelective(shopAddress);
        }else{
            num=insert(shopAddress);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
}
